﻿@model AnalysisApp.Web.Models.ConfigurationModel

@{
    ViewBag.Title = "Configuration";
}

<h2>Configuration</h2>
<script type="text/javascript">

    var connectionInfo = null;
    
    
    
    function setOLAPConnectionStringAndCheck() {
        var connectionString = $('#taConnectionString').val();
        var cs = connectionString ? "OLAPConnectionString=" + connectionString : '';
        $.ajax({
            type: 'POST',
            url: '/Home/SetOLAPConnectionStringAndCheck',
            data: {
                'connection': cs
            },
            success: function (msg) {
                _p = arguments;
                if (arguments[2].getResponseHeader('error')) {
                    $('#taResultConnectionTest').val(msg);
                    $('#taResultConnectionTest').css('color', 'red');
                } else {
                    connectionInfo = { ConnectionID: 'OLAPConnectionString', ConnectionString: connectionString };
                    $('#taResultConnectionTest').css('color', 'green');
                    $('#taResultConnectionTest').val(msg);
                    getCubes();
                }
            },
            error: function (msg) {
                alert('Something went wrong');
            }
        });
    }

    function getCubes() {
        
        if (!connectionInfo) {
            alert('Set connection first');
            return;
        }

        _oo = { queryType: 'GetCubes', connectionInfo: connectionInfo, parameters: [] };
        $.ajax({ type: 'Post', dataType: 'json', url: '/HOME/GetCubes', data: JSON.stringify({ queryType: 'GetCubes', connectionInfo: connectionInfo, parameters: [] }), contentType: 'application/json; charset=utf-8'
    , success: function (data) {
        var options = $.map(data, function (i, x) { return "<option>" + i.Key + "</option>"; }).join(',\n');
        $('#cubes').html('').append(options);

    }, error: function (msg) {
        //window.__f = msg;
        $('#taResultConnectionTest').val('Get cube error - '+msg.responseText);
        $('#taResultConnectionTest').css('color', 'red');
        
    }
        });
        
    }

    

</script>

@using (Html.BeginForm("CreateConfiguration","Home"))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>ConfigurationModel</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.OlapConnectionString)
            To connect to the local cubes use following connection string format: <strong>Provider=MSOLAP.4;Data Source=|DataDirectory|\<i style="color: blueviolet">CubeName</i></strong>. Replace CubeName with the actual file name.
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.OlapConnectionString,new { style="width:100%;height:100px;", id="taConnectionString" })
            @Html.ValidationMessageFor(model => model.OlapConnectionString)
            <input type="button" onclick="setOLAPConnectionStringAndCheck()" value="Set OLAPConnectionString and check it(empty value will use web.config setting)"/>
            

        </div>
        <div class="editor-label">
            <label>Result</label>
        </div>
        <div class="editor-field">
            <textarea id="taResultConnectionTest" style="width: 100%;height: 400px;background-color: rgb(240,240,240);"></textarea>
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.SampleCubeName)
        </div>
        <div class="editor-field">
            @Html.ValidationMessageFor(model => model.SampleCubeName)
            
            <!--    <input type="button" onclick="getCubes()" value="GetCubes"/>-->

            <!--@Ajax.ActionLink("Get cubes", "GetCubes", "Choice", new AjaxOptions() { UpdateTargetId = "s" })-->
            <select id="cubes" name="SampleCubeName" style="width: 300px;">
                <option value="@Model.SampleCubeName" selected>@Model.SampleCubeName</option>
            </select>
           

            <div id="s"></div>
        </div>

        <p>
            <input type="submit" value="Run MDX designer" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to Home", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
